package com.ourteam.workspace.dao.impl;

import com.ourteam.workspace.dao.IWorkspaceDAO;
import com.ourteam.workspace.dao.Workspace;
import com.ourteam.workspace.dao.WorkspaceDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-11-30 18:42:32 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractWorkspaceDAOJdbcImpl extends AbstractSpringDAO implements IWorkspaceDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        CreateTime,
        Name,
        Status,
        WorkspacePath,
        WorkspaceType,
        Description,
        SubmitterId,
        CreatorName,
        LastModifierName,
        LastModifyTime
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.CreateTime,
        _DataObjectProperty.Name,
        _DataObjectProperty.Status,
        _DataObjectProperty.WorkspacePath,
        _DataObjectProperty.WorkspaceType,
        _DataObjectProperty.Description,
        _DataObjectProperty.SubmitterId,
        _DataObjectProperty.CreatorName,
        _DataObjectProperty.LastModifierName,
        _DataObjectProperty.LastModifyTime
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<Workspace, WorkspaceDAOQueryBean> daoHelper;

  public AbstractWorkspaceDAOJdbcImpl() {
    super();
    daoHelper = new DataAccessObjectHelper<Workspace, WorkspaceDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aWorkspace
   * @throws Exception
   */
  public boolean checkIsUnique(Workspace aWorkspace) throws Exception {

    if (aWorkspace == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aWorkspace.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aWorkspace
   * @throws Exception
   */
  public void insert(Workspace aWorkspace) throws Exception {

    super.doInsert(aWorkspace);
  }

  /**
   * Batch Insert Datas
   *
   * @param aWorkspace
   * @throws Exception
   */
  public void batchInsert(Workspace[] aWorkspaces) throws Exception {

    super.doBatchInsert(aWorkspaces);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(WorkspaceDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(Workspace aWorkspace) throws Exception {

    delete(aWorkspace.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aWorkspace
   * @throws Exception
   */
  public void update(Workspace aWorkspace) throws Exception {

    if (aWorkspace.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aWorkspace);

    Workspace tempObj = findById(aWorkspace.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aWorkspace, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aWorkspaces[]
   * @throws Exception
   */
  public void batchUpdate(Workspace[] aWorkspaces) throws Exception {

    super.doBatchUpdate(aWorkspaces);
  }

  /**
   * Update Data By Condictions
   *
   * @param aWorkspace
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(Workspace aWorkspace, WorkspaceDAOQueryBean aQueryBean) throws Exception {

    super.doUpdateByQuery(aWorkspace, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(WorkspaceDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(Workspace aWorkspace) throws Exception {

    if (aWorkspace.getCreateTime() == null) {
      throw new Exception("The Workspace.createTime value is not allow NULL");
    }

    if (aWorkspace.getName() == null) {
      throw new Exception("The Workspace.name value is not allow NULL");
    }

    if (aWorkspace.getStatus() == null) {
      throw new Exception("The Workspace.status value is not allow NULL");
    }

    if (aWorkspace.getWorkspacePath() == null) {
      throw new Exception("The Workspace.workspacePath value is not allow NULL");
    }

    if (aWorkspace.getWorkspaceType() == null) {
      throw new Exception("The Workspace.workspaceType value is not allow NULL");
    }
  }

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public Workspace findById(final long id) throws Exception {

    WorkspaceDAOQueryBean queryBean = new WorkspaceDAOQueryBean();

    WorkspaceDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    Workspace[] Workspaces = query(queryBean);

    if (Workspaces.length == 0) {
      return null;
    } else {
      return Workspaces[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return Workspace[]
   * @throws Exception
   */
  public Workspace[] query(WorkspaceDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcWorkspaceRowMapper());

    return (Workspace[]) result.toArray(new Workspace[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(WorkspaceDAOQueryBean queryBean) throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(WorkspaceDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(Workspace[] aWorkspaces, WorkspaceDAOQueryBean queryBean)
      throws Exception {
    daoHelper.synchronize(aWorkspaces, queryBean);
  }

  public void synchronize(
      Workspace[] aWorkspaces,
      WorkspaceDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<Workspace> callBack)
      throws Exception {
    daoHelper.synchronize(aWorkspaces, queryBean, callBack);
  }

  public static class JdbcWorkspaceRowMapper implements RowMapper<Workspace> {

    public Workspace mapRow(ResultSet resultSet, int arg1) throws SQLException {
      Workspace object = new Workspace();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object createTime = null;

        __dataType = resultSet.getMetaData().getColumnType(columnIndex);

        if (__dataType == java.sql.Types.DATE
            || __dataType == java.sql.Types.TIME
            || __dataType == java.sql.Types.TIMESTAMP) {
          createTime = resultSet.getTimestamp(columnIndex++);

        } else {
          createTime = resultSet.getObject(columnIndex++);
        }

        object.setCreateTimeNull(resultSet.getBoolean(columnIndex++));

        if (createTime != null) {

          setBeanValue(object, _DataObjectProperty.CreateTime, createTime);
        }

        Object name = null;

        name = resultSet.getObject(columnIndex++);

        object.setNameNull(resultSet.getBoolean(columnIndex++));

        if (name != null) {

          setBeanValue(object, _DataObjectProperty.Name, name);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object workspacePath = null;

        workspacePath = resultSet.getObject(columnIndex++);

        object.setWorkspacePathNull(resultSet.getBoolean(columnIndex++));

        if (workspacePath != null) {

          setBeanValue(object, _DataObjectProperty.WorkspacePath, workspacePath);
        }

        Object workspaceType = null;

        workspaceType = resultSet.getObject(columnIndex++);

        object.setWorkspaceTypeNull(resultSet.getBoolean(columnIndex++));

        if (workspaceType != null) {

          setBeanValue(object, _DataObjectProperty.WorkspaceType, workspaceType);
        }

        Object description = null;

        description = resultSet.getObject(columnIndex++);

        object.setDescriptionNull(resultSet.getBoolean(columnIndex++));

        if (description != null) {

          setBeanValue(object, _DataObjectProperty.Description, description);
        }

        Object submitterId = null;

        submitterId = resultSet.getObject(columnIndex++);

        object.setSubmitterIdNull(resultSet.getBoolean(columnIndex++));

        if (submitterId != null) {

          setBeanValue(object, _DataObjectProperty.SubmitterId, submitterId);
        }

        Object creatorName = null;

        creatorName = resultSet.getObject(columnIndex++);

        object.setCreatorNameNull(resultSet.getBoolean(columnIndex++));

        if (creatorName != null) {

          setBeanValue(object, _DataObjectProperty.CreatorName, creatorName);
        }

        Object lastModifierName = null;

        lastModifierName = resultSet.getObject(columnIndex++);

        object.setLastModifierNameNull(resultSet.getBoolean(columnIndex++));

        if (lastModifierName != null) {

          setBeanValue(object, _DataObjectProperty.LastModifierName, lastModifierName);
        }

        Object lastModifyTime = null;

        __dataType = resultSet.getMetaData().getColumnType(columnIndex);

        if (__dataType == java.sql.Types.DATE
            || __dataType == java.sql.Types.TIME
            || __dataType == java.sql.Types.TIMESTAMP) {
          lastModifyTime = resultSet.getTimestamp(columnIndex++);

        } else {
          lastModifyTime = resultSet.getObject(columnIndex++);
        }

        object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

        if (lastModifyTime != null) {

          setBeanValue(object, _DataObjectProperty.LastModifyTime, lastModifyTime);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
